package com.lixin.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lixin.entity.GroupMember;
import com.lixin.mapper.GroupMemberMapper;
import com.lixin.service.IGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lixin
 * @since 2023-08-08
 */
@Service
public class GroupMemberServiceImpl extends ServiceImpl<GroupMemberMapper, GroupMember> implements IGroupMemberService {

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Override
    public GroupMember addOrUpdate(GroupMember groupMember) {
        if (groupMember.getId() == null) {
            groupMemberMapper.insert(groupMember);
        } else {
            groupMemberMapper.updateById(groupMember);
        }
        return groupMember;
    }


    @Override
    public GroupMember builderMember(Integer groupId, boolean isAdmin, Integer userId, Date date) {
        //创建成员并设置管理员
        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(groupId);
        groupMember.setIsAdmin(isAdmin);
        groupMember.setUserId(userId);
        groupMember.setJoinTime(date);
        this.addOrUpdate(groupMember);
        return groupMember;
    }

    @Override
    public List<GroupMember> selectMemberByGroupId(Integer groupId) {
        return this.lambdaQuery().eq(GroupMember::getGroupId, groupId).list();
    }

    @Override
    public GroupMember selectByUserId(Integer userId) {
        return this.lambdaQuery().eq(GroupMember::getUserId, userId).one();

    }

    @Override
    public Integer deleteByGroupId(Integer groupId) {
        QueryChainWrapper<GroupMember> wrapper = this.query().eq("group_id", groupId);
        return groupMemberMapper.delete(wrapper);
    }
}
